const Joi = require('joi');


//定义验证规则
const schema = Joi.object({
    username: Joi.string().min(2).max(10).required().error(new Error('username属性验证失败'))
});
//该方法返回promise对象，可通过异步函数try-catch方式获取结果
async function run() {
    try {
        //实施验证
        await schema.validateAsync({ username: 'ab' });
    } catch (ex) {
        console.log(ex.message);
        return;
    }
    console.log('验证成功');
}
run();